;
(function(call) {
    var main = {
        userInfo: null,
        init: function() {
            this.initView();
            this.bindEvt();
        },
        initView: function() {
            this.initTime()
            var userInfo = $("#userInfo").data('info');
            this.userInfo = userInfo;
            var type = this.getUserType();
            if (type == 1) {
                $("#admin").show();
                $("#forecaster").hide();
                this.getGather()
            } else if (type == 2) {
                this.getLatest()
            } else {
                $("#admin").show();
                $("#forecaster,.addTask").hide();
                this.getGather()
            }
        },
        initTime: function() {
            var startDate = new Date();
            startDate.setMonth(startDate.getMonth() - 1);
            var endDate = new Date();
            endDate.setMonth(endDate.getMonth() + 1);
            laydate = layui.laydate;
            laydate.render({
                elem: '#startTime',
                type: 'datetime',
                value: new Date(),
                format: 'yyyy-MM-dd HH'
            });
            laydate.render({
                elem: '#endTime',
                type: 'datetime',
                value: new Date(),
                format: 'yyyy-MM-dd HH'
            });
            laydate.render({
                elem: '#correTime',
                type: 'datetime',
                value: new Date(),
                format: 'yyyy-MM-dd HH'
            });
            laydate.render({
                elem: '#startTime2',
                type: 'datetime',
                value: startDate,
                format: 'yyyy-MM-dd HH:mm'
            });
            laydate.render({
                elem: '#endTime2',
                type: 'datetime',
                value: endDate,
                format: 'yyyy-MM-dd HH:mm'
            });
            laydate.render({
                elem: '#addTaskTime',
                type: 'datetime',
                value: new Date(),
                format: 'yyyy-MM-dd HH:mm'
            });
        },
        // 获取用户类型//type=1超级管理员，2为首席，3其他
        getUserType: function() {
            var type = 3;
            if (this.userInfo.smuser.iid == '2') {
                type = 1;
                return type;
            }
            if (this.userInfo.postIds.length > 0) {
                for (var i = 0; i < this.userInfo.postIds.length; i++) {
                    if (this.userInfo.postIds[i] == 3) {
                        type = 2;
                    }
                }
            }
            return type;
        },
        // 初始化最新一条数据
        getLatest: function() {
            var that = this;
            $.getJSON(api.internal.gdyb.getNewLatest, {
                userId: that.userInfo.smuser.iid
            }, function(res) {
                var data = res.data;
                if (data.taskId) {
                    var curTime = new Date().format('yyyy-MM-dd hh');
                    $(".consultationName").text(data.taskName).data('data', data)
                    $(".consultationTime").text(data.taskDate ? new Date(data.taskDate).format('yyyy-MM-dd hh:mm') : '')
                    $("#startTime").val(data.startTime ? new Date(data.startTime).format('yyyy-MM-dd hh') : curTime);
                    $("#endTime").val(data.endTime ? new Date(data.endTime).format('yyyy-MM-dd hh') : curTime);
                    $("#correTime").val(data.maxTime ? new Date(data.maxTime).format('yyyy-MM-dd hh') : curTime);
                    $("#processMax .min").val(data.min);
                    $("#maxRain .min").val(data.minYuqiang);
                    $("#rainAverge .min").val(data.rainAverageMin);
                    $("#processMax .max").val(data.max);
                    $("#maxRain .max").val(data.maxYuqiang);
                    $("#rainAverge .max").val(data.rainAverageMax);
                    if (data.disasterType) {
                        $(".weatherSelect .fs-label").html(data.disasterType);
                        var weatherVal = data.disasterType;
                        weatherVal = weatherVal.split("，")
                        var $div = $(".weatherSelect").find(".fs-option")
                        for (var i = 0; i < weatherVal.length; i++) {
                            $div.each(function() {
                                var val = $(this).attr("data-value");
                                if (weatherVal[i] == val) {
                                    $(this).addClass("selected");
                                }
                            })
                        }
                    }
                    if (data.areas) {
                        $(".citySelect .fs-label").html(data.areas);
                        var cityVal = data.areas
                        cityVal = cityVal.split("，")
                        for (var i = 0; i < cityVal.length; i++) {
                            $(".citySelect").find(".fs-option").each(function() {
                                var val = $(this).attr("data-value");
                                if (cityVal[i] == val) {
                                    $(this).addClass("selected");
                                }
                            })
                        }
                    }
					$("#admin").hide();
					$("#forecaster").show();
                } else {
                    $("#forecaster,.delete,.addTask").hide();
                    $("#admin").show();
                    that.getGather();
                }
            });
        },
        // 发布
        savePublish: function() {
            var that = this;
            var taskDate = $(".consultationName").data('data');
            var areas = $(".citySelect .fs-label").html();
            var disasterType = $(".weatherSelect .fs-label").html();
            var endTime = $("#endTime").val() + ':00:00';
            var min = $("#processMax .min").val();
            var minYuqiang = $("#maxRain .min").val();
            var rainAverageMin = $("#rainAverge .min").val();
            var max = $("#processMax .max").val();
            var maxYuqiang = $("#maxRain .max").val();
            var rainAverageMax = $("#rainAverge .max").val();
            var maxTime = $("#correTime").val() + ':00:00';
            var startTime = $("#startTime").val() + ':00:00';
            var taskId = taskDate.taskId;
            ybyId = this.userInfo.smuser.iid;
            ybyName = this.userInfo.smuser.name;
            if (!areas || !disasterType || !endTime || !min || !minYuqiang || !rainAverageMin || !max || !rainAverageMax || !maxYuqiang || !startTime || !maxTime) {
                layui.layer.msg("不能为空值");
                return;
            }
            var param = {
                areas: areas,
                disasterType: disasterType,
                endTime: endTime,
                min: min,
                minYuqiang: minYuqiang,
                rainAverageMin: rainAverageMin,
                max: max,
                maxYuqiang: maxYuqiang,
                rainAverageMax: rainAverageMax,
                maxTime: maxTime,
                startTime: startTime,
                taskId: taskId,
                ybyId: ybyId,
                ybyName: ybyName
            };
            $.ajax({
                type: "POST",
                url: api.internal.gdyb.getPublish,
                contentType: "application/json;charset=utf-8",
                dataType: 'json',
                data: JSON.stringify(param),
                success: function(res) {
                    if (res.isSuccess) {
                        layui.layer.msg("发布成功", { offset: '340px' });
                    } else {
                        layui.layer.msg("发布失败", { offset: '340px' });
                    }
                }
            })
        },
        // 查询任务页面
        getGather: function() {
            var that = this
            $.getJSON(api.internal.gdyb.getGather, {
                meetTaskName: $(".mattersName").val(),
                taskDateStart: $("#startTime2").val() + ':00',
                taskDateEnd: $("#endTime2").val() + ':00'
            }, function(res) {
                var data = res.data;
                var corridor = $(".corridor-content");
                for (var i = 0; i < data.length; i++) {
                    var dataObj = data[i];
                    var templete = '首席岗天气会商意见表_' + dataObj.taskName + '_' + dataObj.taskDate;
                    var li = '<li>'
                    li += '<p title=' + templete + '>' + templete + '</p><span class="preview">预览</span>'
                    if (dataObj.taskStatus === 0 && that.getUserType() == 1) {
                        li += '<span class="delete">删除</span>'
                    }
                    li += '<span class="download">下载</span>'
                    li += '</li>'
                    var $li = $(li);
                    $li.data('pro', dataObj);
                    corridor.append($li);
                }
            });
        },

        // 管理员新增任务
        addTasks: function() {
            var that = this;
            var matters = $(".addTask-cause input").val();
            var time = $("#addTaskTime").val() + ':00';
            var param = {
                taskDate: time,
                taskName: matters,
                taskStatus: 0,
            };
            $.ajax({
                type: "POST",
                url: api.internal.gdyb.saveMeetingTask,
                contentType: "application/json;charset=utf-8",
                dataType: 'json',
                data: JSON.stringify(param),
                success: function(res) {
                    if (res.isSuccess) {
                        layui.layer.msg("保存成功");
                        $('.query').click();
                    } else {
                        layui.layer.msg(res.message);
                    }
                }
            })
        },
        // 管理员删除任务
        delMeetingTask: function(taskObj) {
            var taskId = taskObj.data('pro').id;
            var param = {
                id: taskId
            };
            $.ajax({
                type: "get",
                url: api.internal.gdyb.delMeetingTask,
                contentType: "application/json;charset=utf-8",
                dataType: 'json',
                data: param,
                success: function(res) {
                    if (res.isSuccess) {
                        layui.layer.msg("删除成功", { offset: '300px' });
                        taskObj.remove();
                    } else {
                        layui.layer.msg("删除失败", { offset: '300px' });
                    }
                }
            })
        },
        // 产品预览
        productPreview: function(filePath) {
            $("#contentFrame").attr("src", "http://10.226.120.202:8088//ioffice/officeView.do?path=" + encodeURI(encodeURI(filePath)));
        },
        bindEvt: function() {
            var that = this;
            // 新增任务页面
            $(".addTask").click(function() {
                $(this).addClass("active")
                layui.layer.open({
                    type: 1,
                    title: '新增任务',
                    area: ['480px', '250px'],
                    content: $(".addTask-content"),
                    shade: 0,
                    skin: 'addTaskTc',
                    resize: false,
                    id: "addTaskTc",
                    success: function() {
                        $(".layui-layer-close").removeAttr("href");
                    },
                    end: function() {
                        $(".addTask").removeClass("active");
                    }
                })
            });
            // 查询任务
            $(".query").click(function() {
                $(".corridor-content").empty()
                that.getGather();
            });
            // 管理员新增任务
            $(".defineBtn").click(function() {
                that.addTasks();
                $(".layui-layer-close").trigger("click")
            });
            // 关闭新增任务弹窗
            $(".cancelBtn").click(function() {
                $(".layui-layer-close").trigger("click")
            });
            // 管理员删除任务
            $(".corridor-content").on('click', 'li .delete', function() {
                var taskObj = $(this).parent();
                that.delMeetingTask(taskObj);
            }).on('click', 'li .preview', function() {
                var proInfo = $(this).parent().data('pro');
                if (!proInfo.wordPath) {
                    layui.layer.msg('没有生成产品');
                    return;
                }
                that.productPreview(proInfo.wordPath);
            }).on('click', 'li .download', function() {
                var proInfo = $(this).parent().data('pro');
                if (!proInfo.wordPath) {
                    layui.layer.msg('没有生成产品');
                    return;
                }
                $('#dowloadFrame').remove();
                var fm = $('<iframe id="dowloadFrame">');
                fm.css({
                    'display': 'none',
                }).attr({
                    'src': api.internal.common.commonDownLoad + '?path='+ encodeURI(proInfo.wordPath)
                });
                $("body").append(fm);
            });
            // 多选框
            $('.city,.weather').fSelect();
            // 下拉复选框
            $(".all-election input").click(function() {
                if ($(this).is(':checked')) {
                    var proNm = []
                    $(".citySelect").find('.fs-option').addClass("selected")
                    $(".citySelect .fs-options").children(".fs-option").each(function() {
                        var val = $(this).attr("data-value")
                        proNm.push(val)
                    })
                    $(".citySelect .fs-label").html(proNm.join('，'))
                } else {
                    $(".citySelect .fs-options").find('.fs-option').removeClass("selected")
                    $(".citySelect .fs-label").html('');
                }
            });
            // 发布接口
            $(".publish").click(function() {
                var min = $("#rainAverge .min").val();
                var max = $("#rainAverge .max").val();
                var proMin = $("#processMax .min").val();
                var proMax = $("#processMax .max").val();
                var rainMin = $("#maxRain .min").val();
                var rainMax = $("#maxRain .max").val();
                if (min > max || proMin > proMax || rainMin > rainMax) {
                    layui.layer.msg('最小值不能大于最大值')
                    return;
                }
                if ((max - min) > 30 || (proMax - proMin) > 30 || (rainMax - rainMin) > 30) {
                    layui.layer.msg('最小值与最大值范围不能超过30')
                    return;
                }
                that.savePublish();
            });
            $(".max").bind('input propertychange', function() {
                var min = $(this).parent().find('.min').val();
                var max = $(this).val();
                if (!$.isNumeric(min) || !$.isNumeric(max)) return;
                min = Number(min);
                max = Number(max);
                if (max < 0 || max < min || (max - min) > 30) {
                    $(this).addClass("redBorder");
                } else {
                    $(this).removeClass("redBorder");
                    $('.min').removeClass("redBorder");
                }
            })
            $(".min").bind('input propertychange', function() {
                var max = $(this).parent().find('.max').val();
                var min = $(this).val();
                if (!$.isNumeric(min) || !$.isNumeric(max)) return;
                min = Number(min);
                max = Number(max);
                if (min < 0 || min > max || (max - min) > 30) {
                    $(this).addClass("redBorder");
                } else {
                    $(this).removeClass("redBorder");
                    $('.max').removeClass("redBorder");
                }
            })
			$('input').attr("autocomplete","off")
        }
    };
    if (typeof call === 'function') {
        call(main);
    }
})(function (main) {
	main.init();
})